<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="事件时间">
        <el-date-picker
          v-model="daterangeEventTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="事件类型" prop="eventType">
        <el-select v-model="queryParams.eventType" placeholder="请选择事件类型" clearable>
          <el-option
            v-for="dict in dict.type.sl_event_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="事件级别" prop="eventLevel">
        <el-select v-model="queryParams.eventLevel" placeholder="请选择事件级别" clearable>
          <el-option
            v-for="dict in dict.type.sl_event_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="eventSubject">
        <el-input
          v-model="queryParams.eventSubject"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="isDone">
        <el-select v-model="queryParams.isDone" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sl_event_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sl:event:add']"
        >新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="eventId" />
      <el-table-column label="事件时间" align="center" prop="eventTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eventTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事件类型" align="center" prop="eventType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_event_type" :value="scope.row.eventType"/>
        </template>
      </el-table-column>
      <el-table-column label="事件级别" align="center" prop="eventLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_event_level" :value="scope.row.eventLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="eventSubject" />
      <el-table-column label="审核人" align="center" prop="eventNick" />
      <el-table-column label="汇报时间" align="center" prop="submitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isDone">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_event_status" :value="scope.row.isDone"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sl:event:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.isDone =='0' "
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sl:event:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改情况汇报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="事件时间" prop="eventTime">
          <el-date-picker clearable
            v-model="form.eventTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择事件时间">
          </el-date-picker>
        </el-form-item>
        <el-row>
          <el-col :span="12">
           <el-form-item label="事件类型" prop="eventType">
             <el-select v-model="form.eventType" placeholder="请选择事件类型">
               <el-option
                 v-for="dict in dict.type.sl_event_type"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
               ></el-option>
             </el-select>
           </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="事件级别" prop="eventLevel">
              <el-select v-model="form.eventLevel" placeholder="请选择事件级别">
                <el-option
                  v-for="dict in dict.type.sl_event_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="审核人" prop="eventNick">
          <el-input v-model="form.eventNick" readonly="readonly" placeholder="请选择审核人" >
            <el-button slot="append" @click="handleUser2Select" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <UserSingleSelect ref="user2Select" @onSelected="onUser2Selected"></UserSingleSelect>
        <el-form-item label="标题" prop="eventSubject">
          <el-input v-model="form.eventSubject" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="eventContent">
          <el-input v-model="form.eventContent" type="textarea" :autosize="{ minRows: 6}" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="draftForm" v-if="form.isDone =='0' ">保存草稿</el-button>
        <el-button type="primary" @click="submitForm" v-if="form.isDone =='0' ">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvent, getEvent, delEvent, addEvent, updateEvent } from "@/api/sl/event";
import UserSingleSelect from "@/components/userSelect/single.vue"

export default {
  name: "Event",
  components: {UserSingleSelect},
  dicts: ['sl_event_status', 'sl_event_level', 'sl_event_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 情况汇报表格数据
      eventList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态时间范围
      daterangeEventTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eventTime: null,
        eventType: null,
        eventLevel: null,
        eventSubject: null,
        isDone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        eventTime: [
          { required: true, message: "事件时间不能为空", trigger: "blur" }
        ],
        eventType: [
          { required: true, message: "事件类型不能为空", trigger: "change" }
        ],
        eventLevel: [
          { required: true, message: "事件级别不能为空", trigger: "change" }
        ],
        eventSubject: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        eventContent: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        eventNick: [
          { required: true, message: "审核人不能为空", trigger: "blur" }
        ],
 
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询情况汇报列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeEventTime && '' != this.daterangeEventTime) {
        this.queryParams.params["beginEventTime"] = this.daterangeEventTime[0];
        this.queryParams.params["endEventTime"] = this.daterangeEventTime[1];
      }
      listEvent(this.queryParams).then(response => {
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        eventId: null,
        eventTime: null,
        eventType: null,
        eventLevel: null,
        eventSubject: null,
        eventContent: null,
        eventUser: null,
        submitTime: null,
        isDone: "0",
        eventNick: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEventTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.eventId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加情况汇报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const eventId = row.eventId || this.ids
      getEvent(eventId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改情况汇报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.isDone = "1"
          if (this.form.eventId != null) {
            updateEvent(this.form).then(response => {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvent(this.form).then(response => {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 保存草稿按钮 */
    draftForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.isDone = "0"
          if (this.form.eventId != null) {
            updateEvent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvent(this.form).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const eventIds = row.eventId || this.ids;
      this.$modal.confirm('是否确认删除情况汇报编号为"' + eventIds + '"的数据项？').then(function() {
        return delEvent(eventIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sl/event/export', {
        ...this.queryParams
      }, `event_${new Date().getTime()}.xlsx`)
    },
    //点击人员选择按钮
    handleUser2Select(){
        this.$refs.user2Select.showFlag = true;
    },
    //人员选择返回
    onUser2Selected(row){
        this.form.eventUser = row.userName;
        this.form.eventNick = row.nickName;
    },
  }
};
</script>
